home *** CD-ROM | disk | FTP | other *** search
/ MIDICraft's MIDINET CD-ROM / MIDICraft's MIDINET CD-ROM.iso / DOSUTILS / KORG / MPU.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-29  |  2.4 KB  |  167 lines

  1. #include "mpu.hpp"
  2. #include <conio.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. int dataport = 0x330;
  7. int statusport = 0x331;
  8. char uartmode = 0;
  9.  
  10. MPU::MPU()
  11. {
  12.   uartmode = 0;
  13. }
  14.  
  15. MPU::~MPU()
  16. {}
  17.  
  18.  
  19. Soundcard* MPU::recognize()
  20. {
  21. char* env = getenv("MPU") ;
  22.  
  23.   if (env)
  24.   {
  25.   int portno;
  26.  
  27.     if (sscanf(env, "%x", &portno) == 1)
  28.     {
  29.       dataport = portno;
  30.       statusport = portno+1;
  31.       if (resetmpu())
  32.     return new MPU;
  33.     }
  34.   }
  35.   dataport = 0x330;
  36.   statusport = 0x331;
  37.   if (resetmpu())
  38.     return new MPU;
  39.   dataport = 0x300;
  40.   statusport = 0x301;
  41.   if (resetmpu())
  42.     return new MPU;
  43.   return 0; // not recognized
  44. }
  45.  
  46. int MPU::reset()
  47. {
  48.   return resetmpu();
  49. }
  50.  
  51. int MPU::resetmpu()
  52. {
  53.   uartmode = 0;
  54.   if (!putmpu(statusport, 0xff))
  55.     return 0;
  56.   return 1;
  57. }
  58.  
  59. void MPU::startinput()
  60. {
  61.   setuart();
  62. }
  63.  
  64. void MPU::stopinput()
  65. {
  66.   reset();
  67. }
  68.  
  69. int MPU::getmpu(int port, int aftertimeout)
  70. {
  71.   register unsigned i;
  72.   for (i = 0xffffU; i; i--)
  73.   if (!isinput())
  74.     break;
  75.   if (i == 0 && !aftertimeout)
  76.     return -1;
  77.   return inp(port);
  78. }
  79.  
  80. int MPU::putmpu(int port, unsigned char ch)
  81. {
  82.   register unsigned i = 0xffff;
  83.   for (i = 0xffffU; i; i--)
  84.   if (!isoutput())
  85.     break;
  86.   if (i == 0 && ch != 0xff)// except for reset command
  87.     return 0;  // not allowed
  88.   // send command  (ff = reset)
  89.   outp(port, ch);
  90.  
  91.   if (port == statusport) // wait for 0xfe response
  92.   {
  93.     for (i = 0xffffU; i; i--)
  94.     if (!isinput())
  95.     {
  96.       if (inp(dataport) == 0xfe)
  97.     break;
  98.     }
  99.     if (i == 0)
  100.       return 0;
  101.   }
  102.   return 1;
  103. }
  104.  
  105. int MPU::getbyte()
  106. {
  107.   return getmpu(dataport);
  108. }
  109.  
  110. int MPU::putbyte(unsigned char c)
  111. {
  112.   return putmpu(dataport, c);
  113. }
  114.  
  115. int MPU::isoutput()
  116. {
  117.   return inp(statusport) & 0x40;
  118. }
  119.  
  120. int MPU::isinput()
  121. {
  122.   return inp(statusport) & 0x80;
  123. }
  124.  
  125. int MPU::hear(unsigned char* buf, int maxlen)
  126. {
  127. int n = 0;
  128. int c;
  129.  
  130.   setuart();
  131.  
  132.   while ( n < maxlen)
  133.   {
  134.     c = getmpu(dataport);
  135.     if (c == -1)
  136.       break;
  137.     *buf++ = c;
  138.     n++;
  139.   }
  140.   return n;
  141. }
  142.  
  143. int MPU::play(unsigned char* buf, int len)
  144. {
  145. int n = 0;
  146.  
  147.   setuart();
  148.   while (len-- > 0)
  149.   {
  150.     if (!putmpu(dataport, *buf++))
  151.       break;
  152.     n++;
  153.   }
  154.   return n;
  155. }
  156.  
  157. int MPU::setuart()
  158. {
  159.   if (uartmode)
  160.      return 1;
  161.   reset();
  162.   if (!putmpu(statusport, 0x3f))
  163.     return 0;
  164.   uartmode = 1;
  165.   return 1;
  166. }
  167.